package com.musemun.service.function.functionimpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.google.protobuf.Api;
import com.musemun.common.ResultCode;
import com.musemun.exception.ApiException;
import com.musemun.pojo.*;
import com.musemun.pojo.Vo.CrFixVo;
import com.musemun.pojo.Vo.PagingVo;
import com.musemun.service.CrfixifoService;
import com.musemun.service.CrifoService;
import com.musemun.service.CropifoService;
import com.musemun.service.FixcontactService;
import com.musemun.service.function.CulturalRelicFix;
import com.musemun.service.impl.CropifoServiceImpl;
//import com.mysql.cj.jdbc.exceptions.SQLError;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;

/**
 * @description:
 *      文物修复类
 * @author winner
 * @date 2020/4/19
 */
@Service
public class CulturalRelicFixImpl implements CulturalRelicFix {

    // 文物修复表
    @Resource
    private CrfixifoService crfixifoService;

    // 修复联系人表
    @Resource
    private FixcontactService fixcontactService;

    // 操作记录表
    @Resource
    private CropifoService cropifoService;

    @Resource
    private CrifoService crifoService;






    public void setCropifoService(com.musemun.service.CropifoService cropifoService) {
        this.cropifoService = cropifoService;
    }

    public void setFixcontactService(FixcontactService fixcontactService) {
        this.fixcontactService = fixcontactService;
    }

    public void setCrfixifoService(CrfixifoService crfixifoService) {
        this.crfixifoService = crfixifoService;
    }



    /**
     *
     * @param crNo
     *
     * @return
     */
    public CrFixVo findById(Long crNo) {
        LambdaQueryWrapper<Crfixifo> lambdaQuery = new QueryWrapper<Crfixifo>().lambda();
        lambdaQuery.eq(Crfixifo::getCrNo, crNo).orderByDesc(Crfixifo::getFSDate);
        List<Crfixifo> crfixifo = crfixifoService.list(lambdaQuery);
        if (crfixifo == null || crfixifo.size() == 0) {
            throw new ApiException(ResultCode.GREENHOUSE_NOT_FOUND, "编号" + String.valueOf(crNo) + "： 未找到");
        }
        CrFixVo crFixVo = new CrFixVo();
        crFixVo.setCrfixifo(crfixifo.get(0));
        return crFixVo;
    }

    private Crfixifo _findById(Long crNo) {
        LambdaQueryWrapper<Crfixifo> lambdaQuery = new QueryWrapper<Crfixifo>().lambda();
        lambdaQuery.eq(Crfixifo::getCrNo, crNo).orderByDesc(Crfixifo::getFSDate);
        List<Crfixifo> crfixifo = crfixifoService.list(lambdaQuery);
        if (crfixifo == null || crfixifo.size() == 0) {
            throw new ApiException(ResultCode.GREENHOUSE_NOT_FOUND, "编号" + String.valueOf(crNo) + "： 未找到");
        }
        return crfixifo.get(0);
    }

    public PagingVo findByName(String name, int pageNum, int pageSize) {
        PagingVo pagingVo = new PagingVo();
        QueryWrapper<Crfixifo> lambdaQuery = new QueryWrapper<Crfixifo>();
        lambdaQuery.eq("FCName",name);
        if (pageNum == 0 && pageSize == 0) {
            pagingVo.setList(crfixifoService.list(lambdaQuery));
        } else {
            Page<Crfixifo> page = new Page<>(pageNum, pageSize);
            IPage<Crfixifo> iPage = crfixifoService.page(page, lambdaQuery);
            pagingVo.setList(iPage.getRecords());
            pagingVo.setNum(iPage.getTotal());
        }
        if (pagingVo.getList() == null) {
            throw new ApiException(ResultCode.DATA_NULL, "未找到任何信息");
        }
        return pagingVo;
    }

    public PagingVo selectAll(int pageNum, int pageSize) {
        PagingVo pagingVo = new PagingVo();
        LambdaQueryWrapper<Crfixifo> lambdaQuery = new QueryWrapper<Crfixifo>().lambda();
        if (pageNum == 0 && pageSize == 0) {
            pagingVo.setList(crfixifoService.list(lambdaQuery));
        } else {
            Page<Crfixifo> page = new Page<>(pageNum, pageSize);
            IPage<Crfixifo> iPage = crfixifoService.page(page, lambdaQuery);
            pagingVo.setList(iPage.getRecords());
            pagingVo.setNum(iPage.getTotal());
        }
        if (pagingVo.getList() == null) {
            throw new ApiException(ResultCode.DATA_NULL, "未找到任何信息");
        }
        return pagingVo;
    }

    @Transactional
    public boolean insert(Crfixifo crfixifo, Fixcontact fixcontact, Cropifo cropifo) {
        Crifo crifo = crifoService.getById(crfixifo.getCrNo());
        if (crifo == null) {
            throw new ApiException(ResultCode.GREENHOUSE_NOT_FOUND, "编号 " + String.valueOf(crfixifo.getCrNo()) + " 文物没找到");
        }
        if (!crifo.getCrState().equals("在馆") && !crifo.getCrState().equals("入库")) {
            throw new ApiException(ResultCode.GREENHOUSE_NOT_FOUND, "编号 " + String.valueOf(crfixifo.getCrNo()) + " 文物不在馆！！！！！！！！");
        }
        if (!fixcontact.getFCName().equals(crfixifo.getFCName()) || crfixifo.getCrNo().longValue() != cropifo.getCrNo().longValue()) {
            throw new ApiException(ResultCode.DATA_ERROR, "信息不一致");
        }
        if (fixcontactService.getById(crfixifo.getFCName()) == null) {
            if (!fixcontactService.save(fixcontact)) {
                throw new ApiException(ResultCode.ERROR, "插入修复联系人时，出错");
            }
        }
        Employee employee = cropifoService.getEmployee();
        cropifo.setENo(employee.getENo());
        if (!cropifoService.save(cropifo)) {
            throw new ApiException(ResultCode.ERROR, "插入操作记录行时错误");
        }

        if (!crfixifoService.save(crfixifo)) {
            throw new ApiException(ResultCode.ERROR, "插入修复行时错误");
        }
        UpdateWrapper<Crifo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("crNo", crfixifo.getCrNo());
        crifo.setCrState(cropifo.getOpType());

        if (!crifoService.update(crifo, updateWrapper)) {
            throw new ApiException(ResultCode.ERROR, "文物状态改变失败");
        }
        return true;
    }

    @Transactional
    public boolean delete(long crNo) {
        Crfixifo crfixifo = this._findById(crNo);
        LambdaQueryWrapper<Crfixifo> lambdaQuery1 = new QueryWrapper<Crfixifo>().lambda();
        lambdaQuery1.eq(Crfixifo::getCrNo, crNo);
        if (!crfixifoService.remove(lambdaQuery1)) {
            throw new ApiException(ResultCode.ERROR, "删除修复信息行时失败");
        }


        return true;
    }

    public CrFixVo update(long crNo) {
        Crfixifo crfixifo = this._findById(crNo);
        Fixcontact fixcontact = fixcontactService.getById(crfixifo.getFCName());
        if (fixcontact == null) {
            throw new ApiException(ResultCode.ERROR, "未找到任何信息");
        }
        CrFixVo crFixVo = new CrFixVo();
        crFixVo.setFixcontact(fixcontact);
        crFixVo.setCrfixifo(crfixifo);
        return crFixVo;
    }

    @Transactional
    public boolean update(Crfixifo crfixifo, Fixcontact fixcontact) {
        if (!fixcontact.getFCName().equals(crfixifo.getFCName())) {
            throw new ApiException(ResultCode.DATA_ERROR, "数据不一致");
        }
        if (!fixcontactService.saveOrUpdate(fixcontact)) {
            throw new ApiException(ResultCode.ERROR, "更新修复联系人行时失败");
        }
        if (!crfixifoService.updateById(crfixifo)) {
            throw new ApiException(ResultCode.ERROR, "更新修复信息行时失败");
        }
        return true;
    }

    public List<Crfixifo> findByTime(Date leftTime, Date rightTime, int operate) {
        QueryWrapper<Crfixifo> lambdaQuery = new QueryWrapper<Crfixifo>();
        if (operate == 1) {
            lambdaQuery.lt("FSDate", leftTime);
        } else if (operate == 2) {
            if (rightTime == null) {
                throw new ApiException(ResultCode.DATA_ERROR, "参数righttime错误");
            }
            lambdaQuery.between("FSDate", leftTime, rightTime);
        } else if (operate == 3){
            lambdaQuery.gt("FSDate", leftTime);
        } else {
            throw new ApiException(ResultCode.DATA_ERROR, "参数operate参数");
        }
        List<Crfixifo> list = crfixifoService.list(lambdaQuery);
        if (list == null) {
            throw new ApiException(ResultCode.DATA_NULL, "未找到任何记录");
        }
        return list;
    }

}
